package com.example.administrator.hqq.activity.home;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.bean.GroupEvent;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.example.administrator.hqq.utils.HintWordsUtils;
import com.example.administrator.hqq.utils.SketchTextWatcher;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class SettingGroupTitleActivity extends AppCompatActivity {

    @BindView(R.id.accomplish)
    Button accomplish;
    @BindView(R.id.group_title)
    EditText groupTitle;
    private String TAG=this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_group_title);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initview();
    }

    private void initview() {
        SketchTextWatcher sketchTextWatcher = new SketchTextWatcher(groupTitle, 54);
        groupTitle.addTextChangedListener(sketchTextWatcher);
        groupTitle.setText(getIntent().getStringExtra(LoongsConfig.group_title));
    }

    @OnClick({R.id.accomplish,R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId())
        {
            case R.id.cancel:
                finish();
                break;
            case R.id.accomplish:
                if (!TextUtils.isEmpty(groupTitle.getText())&&groupTitle.getText().toString().length()>=2)
                {
                    GetBuilder url = OkHttpUtils.get().url(UrlFactory.setGroupTitle());
                    url.addParams(LoongsConfig.group_id,getIntent().getStringExtra(LoongsConfig.group_id))
                            .addParams(LoongsConfig.user_id,LoongsConfig.getUser_id())
                            .addParams(LoongsConfig.title,groupTitle.getText().toString())
                            .build().execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.e(TAG, "onError: "+e.getMessage() );
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.e(TAG, "onResponse: "+response );
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                HintWordsUtils.showMarkeWords(jsonObject.getInt(LoongsConfig.code),jsonObject.getString(LoongsConfig.description));
                                if (jsonObject.getInt(LoongsConfig.code)==200)
                                {
                                    GroupEvent groupEvent = new GroupEvent();
                                    groupEvent.setType(GroupEvent.Type.ALTER_GROUP);
                                    EventBus.getDefault().post(groupEvent);
                                    finish();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }else{
                    ToastUtils.showShort("输入不合法");
                }
                break;
        }

    }

    @Subscribe (threadMode = ThreadMode.MAIN)
    public void Event(String s){

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}